package com.cos.shop.web.controller;

import com.cos.shop.common.Const;
import com.cos.shop.common.ResponseCode;
import com.cos.shop.common.ResponseResult;
import com.cos.shop.entity.Item;
import com.cos.shop.entity.User;
import com.cos.shop.services.AddressServices;
import com.cos.shop.services.CartServices;
import com.cos.shop.services.ItemServices;
import com.cos.shop.services.OrderServices;
import com.cos.shop.services.impl.AddressServicesImpl;
import com.cos.shop.services.impl.CartServicesImpl;
import com.cos.shop.services.impl.ItemServicesImpl;
import com.cos.shop.services.impl.OrderServicesImpl;
import com.cos.shop.util.StringUtils;
import com.cos.shop.vo.ItemProductVo;
import com.cos.shop.vo.OrderDetailVo;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: COS
 * @time: 2023/5/12 8:58
 * @description:
 */
@WebServlet("/order")
public class OrderController extends BaseServlet {
    private CartServices cs = new CartServicesImpl();
    private AddressServices as = new AddressServicesImpl();
    private OrderServices os = new OrderServicesImpl();

    private ItemServices is = new ItemServicesImpl();

    public String changeStatus(HttpServletRequest request, HttpServletResponse response) {
        String oid = request.getParameter("oid");
        ResponseResult rr = os.changeStatus(oid);
        request.setAttribute(Const.MSG,rr);
        return Const.MESSAGE;
    }

    public String getOrderDetail(HttpServletRequest request, HttpServletResponse response) {
        String oid = request.getParameter("oid");
        // 不使用统一返回数据类型
        List<ItemProductVo> itemList = is.getItemListByOid(oid);
        OrderDetailVo detailVo = os.getOrderDetail(oid);
        // reqeust
        //Map<String,Object> map = new HashMap<>();
        //map.put("detailVo",detailVo);
        //map.put("itemList",itemList);
        //request.setAttribute("data", map);
        request.setAttribute("detailVo", detailVo);
        request.setAttribute("itemList", itemList);
        return Const.FORWARD + "orderDetail.jsp";
    }

    public String getOrderList(HttpServletRequest request, HttpServletResponse response) {
        User user = (User) request.getSession().getAttribute(Const.LOGIN_INFO);
        ResponseResult rr = os.getOrderList(user.getUid());
        request.setAttribute("rr", rr);
        return Const.FORWARD + "orderList.jsp";
    }

    public String addOrder(HttpServletRequest request, HttpServletResponse response) {
        int aid = StringUtils.stringToInt(request.getParameter("aid"));
        User user = (User) request.getSession().getAttribute(Const.LOGIN_INFO);
        //访问业务层 , 把新增的order数据设置rr 里面
        ResponseResult rr = os.addOrder(aid, user.getUid());
        if (rr.getCode() == ResponseCode.SUCCESS.getCode()) {
            request.setAttribute("rr", rr);
            // 保存request作用域
            return Const.FORWARD + "orderSuccess.jsp";
        } else {
            request.setAttribute("msg", rr);
            return Const.MESSAGE;
        }
    }

    /**
     * @author: COS
     * @time: 2023/5/12 8:58
     * @description: 添加收货地址, 显示购物车信息, 让用户选择收货地址
     */
    public String getOrderView(HttpServletRequest request, HttpServletResponse response) {
        //1 获取登录用户的id
        User user = (User) request.getSession().getAttribute(Const.LOGIN_INFO);
        //2 访问业务层获取数据
        ResponseResult cartWithProByUid = cs.getCartWithProByUid(user.getUid());
        ResponseResult all = as.getAll(user.getUid());
        //3 存入request作用域中
        request.setAttribute("cartList", cartWithProByUid);
        request.setAttribute("addList", all);
        return Const.FORWARD + "order.jsp";
    }
}
